#include "CBaseView.h"
#include "StringOps.h"
#include"Sonar.h"

using namespace std;

CBaseView::CBaseView():m_pxOut(&std::cout)
{
}

CBaseView::~CBaseView()
{
}

void CBaseView::generateHeader(){
std::string sRelativizer = "uboat/";
	*m_pxOut <<"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"<<endl;
	*m_pxOut <<"<html>"<<endl;
	*m_pxOut <<"<head>"<<endl;
	*m_pxOut <<"<title>" << m_pxRadar->m_xCodebaseRoot.filename()<< " - Neptuner UBoat" "</title>";
	*m_pxOut <<"<link rel='shortcut icon' type='image/ico' href='"<<sRelativizer<<"neptunerIcon.jpg'>"<<endl;
	*m_pxOut <<"		<link rel='stylesheet' type='text/css' media='all' href='uboat/style.css' />"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"prototype.js'></script>"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"fastinit.js'></script>"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"tablesort.js'></script>"<<endl;
	*m_pxOut <<"		<script type='text/javascript' src='"<<sRelativizer<<"codebase.js'></script>"<<endl;
	*m_pxOut <<"	</head>"<<endl;
}

void CBaseView::generateBody()
{
	int nTotalLines=0,nTotalStatements=0,nTotalLoC=0,nTotalLoCom=0, nTotalTokens = 0;
	int nTotalTabs=0,nTotalSpaces=0,nTotalCharacters=0;

	*m_pxOut <<"<body>"<<endl;

	*m_pxOut<< "<script type='text/javascript'>"<<endl;
	*m_pxOut <<"var sCodebaseName='"<<m_pxRadar->m_xCodebaseRoot.filename()<<"';"<<endl;
	*m_pxOut <<"var sCodebaseRoot='"<< m_pxRadar->m_xCodebaseRoot<<"';"<<endl;
	*m_pxOut <<"var sIncludeRoot='"<< m_pxRadar->m_xIncludeRoot<<"';"<<endl;
	*m_pxOut <<"var nFilesParsed="<<m_pxScubaPack->size()<<";"<<endl;
	*m_pxOut<< "</script>"<<endl;

	*m_pxOut <<"<img id='menuNeptuner' src='uboat/neptunerIcon.jpg'></img><strong>Neptuner UBoat</strong>"<<endl;
	*m_pxOut <<"<ul id=usageHelp></ul>"<<endl;
	*m_pxOut <<"<div id=codePod></div>"<<endl;

	*m_pxOut<<"			<table id=codebaseDetails class='sortable'>" <<endl;

	m_asOutputRow.push_back("Location");
	m_asOutputRow.push_back("Filename");
	m_asOutputRow.push_back("Lines");
	m_asOutputRow.push_back("Statements");
	m_asOutputRow.push_back("LoC");
	m_asOutputRow.push_back("Comments");
	m_asOutputRow.push_back("Tokens");
	m_asOutputRow.push_back("Tabs");
	m_asOutputRow.push_back("Spaces");
	m_asOutputRow.push_back("Characters");
	m_asOutputRow.push_back("Complexity");

	printHeading();
	//printFooting(); //if using, need to call printHeading(false)

	Scuba* pxScuba=NULL;
	ScubaPack::iterator i= m_pxScubaPack->begin();
	for(;i!=m_pxScubaPack->end();++i)
	{
		pxScuba = i->second;
		Sonar  xSonar;
		pxScuba->walk(xSonar);

		std::string sFullRelativePath = m_pxRadar->getCodebaseLocation(xSonar.m_pxLocation->m_sFilename);
		std::string sFilename = getFilename(sFullRelativePath);
		std::string sFileHome = getFilehome (sFullRelativePath);
		std::string sTarget = sFullRelativePath + ".uboat.html";
		std::string sAnchor = "<a class='sourcefile' href='codebase/" + sTarget + "'>" ;
		sAnchor += sFilename;
		sAnchor += "</a>";
		m_asOutputRow.push_back(sFileHome);
		m_asOutputRow.push_back(sAnchor);

		m_asOutputRow.push_back(stringify(xSonar.m_nTotalLines)); nTotalLines += xSonar.m_nTotalLines;
		m_asOutputRow.push_back(stringify(xSonar.m_nStatements)); nTotalStatements+= xSonar.m_nStatements;
		m_asOutputRow.push_back(stringify(xSonar.m_nLoC)); nTotalLoC += xSonar.m_nLoC;
		m_asOutputRow.push_back(stringify(xSonar.m_nLoCom)); nTotalLoCom +=xSonar.m_nLoCom;
		m_asOutputRow.push_back(stringify(xSonar.m_asToken.size()));nTotalTokens +=xSonar.m_asToken.size();
		m_asOutputRow.push_back(stringify(xSonar.m_nTabs));nTotalTabs +=xSonar.m_nTabs;
		m_asOutputRow.push_back(stringify(xSonar.m_nSpaces));nTotalSpaces +=xSonar.m_nSpaces;
		m_asOutputRow.push_back(stringify(xSonar.m_nCharacters));nTotalCharacters +=xSonar.m_nCharacters;

		sTarget = sFullRelativePath + ".usub.html";
		sAnchor = "<a href='submarine/" + sTarget + "'>Details</a>" ;
		m_asOutputRow.push_back(sAnchor);

		printElement();
	}
	*m_pxOut<<"			</table>"<<endl;

	*m_pxOut<<"<table id='codebaseStats'>"<<endl;
	m_asOutputRow.push_back("Lines");
	m_asOutputRow.push_back("Statements");
	m_asOutputRow.push_back("LoC");
	m_asOutputRow.push_back("Comments");
	m_asOutputRow.push_back("Tokens");
	m_asOutputRow.push_back("Tabs");
	m_asOutputRow.push_back("Spaces");
	m_asOutputRow.push_back("Characters");
	printHeading();
	m_asOutputRow.push_back(stringify(nTotalLines)); 
	m_asOutputRow.push_back(stringify(nTotalStatements)); 
	m_asOutputRow.push_back(stringify(nTotalLoC)); 
	m_asOutputRow.push_back(stringify(nTotalLoCom)); 
	m_asOutputRow.push_back(stringify(nTotalTokens));
	m_asOutputRow.push_back(stringify(nTotalTabs));
	m_asOutputRow.push_back(stringify(nTotalSpaces));
	m_asOutputRow.push_back(stringify(nTotalCharacters));
	printElement();

	*m_pxOut<<"</table>"<<endl;
}

void CBaseView::generateFooter()
{
	*m_pxOut<<"	</body>"<<endl;
	*m_pxOut<<"</html>"<<endl;
}

void CBaseView::printElement(bool bClear)
{
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<td>"; sEnd = "</td>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;

	if(bClear) m_asOutputRow.clear();

}

void CBaseView::printFooting(bool bClear)
{
	*m_pxOut<<"			<tfoot>"<<endl;
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<td>"; sEnd = "</td>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;
	*m_pxOut<<"			</tfoot>"<<endl;

	if(bClear) m_asOutputRow.clear();
}

void CBaseView::printHeading(bool bClear)
{

	*m_pxOut<<"			<thead>" <<endl;
	*m_pxOut<<"			<tr>";

	int i, n=m_asOutputRow.size();
	std::string sEnd=",", sStart ="";
	sStart ="<th>"; sEnd = "</th>"; 

	for(i=0; i<n; i++)
	{
		*m_pxOut<<sStart<< m_asOutputRow[i]<<sEnd;
	}
	*m_pxOut<<endl;

	*m_pxOut<<"			</tr>"<<endl;
	*m_pxOut<<"			</thead>"<<endl;

	if(bClear) m_asOutputRow.clear();
}

void CBaseView::printBodyStart()
{
	*m_pxOut << "			<tbody>" << endl;
}

void CBaseView::printBodyEnd()
{
	*m_pxOut << "			</tbody>" << endl;
}




